package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserBanner
//@description: 创建UserBanner记录
//@param: userBanner model.UserBanner
//@return: err error

func CreateUserBanner(userBanner model.UserBanner) (err error) {
	err = global.GVA_DB.Create(&userBanner).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserBanner
//@description: 删除UserBanner记录
//@param: userBanner model.UserBanner
//@return: err error

func DeleteUserBanner(userBanner model.UserBanner) (err error) {
	err = global.GVA_DB.Delete(&userBanner).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserBannerByIds
//@description: 批量删除UserBanner记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserBannerByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserBanner{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserBanner
//@description: 更新UserBanner记录
//@param: userBanner *model.UserBanner
//@return: err error

func UpdateUserBanner(userBanner model.UserBanner) (err error) {
	err = global.GVA_DB.Save(&userBanner).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserBanner
//@description: 根据id获取UserBanner记录
//@param: id uint
//@return: err error, userBanner model.UserBanner

func GetUserBanner(id uint) (err error, userBanner model.UserBanner) {
	err = global.GVA_DB.Where("id = ?", id).First(&userBanner).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserBannerInfoList
//@description: 分页获取UserBanner记录
//@param: info request.UserBannerSearch
//@return: err error, list interface{}, total int64

func GetUserBannerInfoList(info request.UserBannerSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserBanner{})
    var userBanners []model.UserBanner
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&userBanners).Error
	return err, userBanners, total
}